package com.canbot.u05.activity.customvoice.tool

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.View

import com.canbot.u05.R
import com.canbot.u05.activity.isNotEmpty2
import kotlinx.android.synthetic.main.dialog_common.*


/**
 * CommonDialog
 *
 * @author Liu.heng
 * @date 2018/5/24.
 */

class CommonDialog constructor(context: Context, themeResId: Int) : Dialog(context, themeResId) {

    private var mContext: Context? = null

    private var content: String? = null

    private var listener: OnCloseListener? = null

    private var positiveName: String? = null

    private var negativeName: String? = null

    private var title: String? = null

    private var isNoTitle = false

    private var isOnlyOne: Boolean = false

    private var isHaveImage = false


    /***
     * @param isOnlyOne 判断弹框底部按钮样式   true 1个   false 2个
     */
    constructor(context: Context, themeResId: Int, content: String, isOnlyOne: Boolean, positiveName: String,
                listener: OnCloseListener) : this(context, themeResId) {
        this.mContext = context
        this.content = content
        this.listener = listener
        this.isOnlyOne = isOnlyOne
        this.positiveName = positiveName
    }


    fun setTitle(title: String) : CommonDialog {
        this.title = title
        return this
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.dialog_common)
        setCanceledOnTouchOutside(false)
        initView()
    }

    private fun initView() {

        cancel.setOnClickListener(::onClick)
        submit.setOnClickListener(::onClick)
        content_Text.text = content
        if (positiveName.isNotEmpty2()) {
            submit.text = positiveName
        }

        if (negativeName.isNotEmpty2()) {
            cancel.text = negativeName
        }

        if (title.isNotEmpty2()) {
            title_Text.text = title
        }

        if (isNoTitle) {
            title_Text.text = ""
        }
        if (isOnlyOne) {
            line_view.visibility = View.GONE
            cancel.visibility = View.GONE
            submit.background = mContext?.getDrawable(R.drawable.training_dialog_bg4)
        }
        if (isHaveImage) {
            my_image.visibility = View.VISIBLE
        }
    }

    private fun onClick(v: View) {
        when (v.id) {
            R.id.cancel -> {
                listener?.onClick(this, false)
                this.dismiss()
            }
            R.id.submit -> listener?.onClick(this, true)

            else -> {
            }
        }
    }

    interface OnCloseListener {

        fun onClick(dialog: Dialog, confirm: Boolean)
    }
}
